#!/usr/bin/env python
# coding: utf-8
import os
from os.path import dirname, exists, expanduser, isdir, join, splitext
import numpy as np
import pandas as pd
from hic_test import load_hic_test
import joblib

print("加载 Health Insurance Costs 测试数据\n")

dataset = load_hic_test()

X = dataset.data
y = dataset.target
headers = dataset.feature_names

print("数据集")
print("已加载 %d 训练数据集, 包含 %d 个特征" % X.shape)
print("预测目标保险客户理赔支出\n")

print("数据集预处理")
categorical_features = ["sex", "region", "smoker"]
X_cat = pd.get_dummies(X[categorical_features])
X = X.drop(categorical_features, axis=1)
X = pd.concat([X, X_cat], axis=1)

print("预处理后 %d 训练数据集, 包含 %d 个特征" % X.shape)

print("\n模型测试")
# from sklearn.linear_model import LinearRegression
# reg = joblib.load('hic-LinearRegression.pkl')
# from sklearn.linear_model import Ridge
# reg = joblib.load('hic-Ridge.pkl')
# from sklearn.linear_model import Lasso
# reg = joblib.load('hic-Lasso.pkl')
# from sklearn.linear_model import ElasticNet
# reg = joblib.load('hic-ElasticNet.pkl')
# from sklearn.linear_model import RidgeCV
# reg = joblib.load('hic-RidgeCV.pkl')
# from sklearn.linear_model import SGDRegressor
# reg = joblib.load('hic-SGDRegressor.pkl')
from sklearn.svm import SVR

def predict(kernel):
    reg = joblib.load('hic-SVR-%s.pkl' % kernel)

    pred_y = reg.predict(X)
    pred_y = pd.Series(pred_y, index=y.index, name="predict_charges")

    pred_y_y = pd.concat([pred_y, y], axis=1)

    print(pred_y_y)
    # 测试结果
    print("\n测试结果")
    print(["max", "min", "avg", "median"])
    # print(model.score(X, y))
    # max(|pred_y - y|)
    # min(|pred_y - y|)
    # max(|pred_y - y| / y)
    # min(|pred_y - y| / y)
    norm_gap = pred_y.sub(y).abs()
    max_norm = norm_gap.max()
    min_norm = norm_gap.min()
    avg_norm = norm_gap.mean()
    med_norm = norm_gap.median()
    print([max_norm, min_norm, avg_norm, med_norm])

    gap_times = norm_gap.div(y)
    max_times = gap_times.max()
    min_times = gap_times.min()
    avg_times = gap_times.mean()
    med_times = gap_times.median()
    print([max_times, min_times, avg_times, med_times])

for kernel in ['linear', 'poly', 'rbf', 'sigmoid']:
    predict(kernel)
